﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace InfoMap.DataAccess
{
    public class UnitOfWork<T> : IDisposable where T : class
    {
        private ObjectContext context { get; set; }
        private Repository<T> repository { get; set; }
        private bool disposed = false;
        private Type contextType { get; set; }


        public UnitOfWork(ObjectContext context)
        {
            this.context = context;
            repository = new Repository<T>();
        }

        public Repository<T> Repository
        {
            get
            {
                if (repository == null)
                    repository = new Repository<T>();
                return repository;
            }
        }

        public void Save()
        {
            context.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
